using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;
namespace DAL
{
	#region Generated code
    public class BlueReportDataAccess : FreshCmsAccessBase
    {
        public List<BlueReport> GetBlueReportList()
        {
            try
            {
                const string spName = "sp_BlueReport_GetList";

                var blueReportList = DbHelper.ExecuteSPList<BlueReport>(spName);
                return blueReportList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return new List<BlueReport>();
            }
        }

        public List<BlueReport> GetBlueReportListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "sp_BlueReport_GetByPaging";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PageIndex", pageIndex),
										new SqlParameter("@PageSize", pageSize),
										new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var blueReportList = DbHelper.ExecuteSqlList<BlueReport>(sqlCommand);
				if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return blueReportList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return new List<BlueReport>();
            }
        }

        public BlueReport GetBlueReportById(int reportId)
        {
            try
            {
                const string spName = "sp_BlueReport_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@ReportId", reportId)
                                    };

                var blueReport = DbHelper.ExecuteSPList<BlueReport>(spName, sqlParams).SingleOrDefault();
                return blueReport;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return null;
            }
        }

        public int InsertBlueReport(BlueReport blueReport)
        {
            try
            {
                const string spName = "sp_BlueReport_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@ReportDatePublish", blueReport.ReportDatePublish),
										new SqlParameter("@ReportTotal", blueReport.ReportTotal),
										new SqlParameter("@ReportNote", blueReport.ReportNote),
										new SqlParameter("@ReportId", blueReport.ReportId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@ReportId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ReportId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@ReportId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return -1;
            }
        }

        public int UpdateBlueReport(BlueReport blueReport)
        {
            try
            {
                const string spName = "sp_BlueReport_Update";
                var sqlParams = new[]
                                    {
										new SqlParameter("@ReportId", blueReport.ReportId)
										, new SqlParameter("@ReportDatePublish", blueReport.ReportDatePublish)
										, new SqlParameter("@ReportTotal", blueReport.ReportTotal)
										, new SqlParameter("@ReportNote", blueReport.ReportNote)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return -1;
            }
        }

        public int DeleteBlueReport(int reportId)
        {
            try
            {
                const string spName = "sp_BlueReport_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@ReportId", reportId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return -1;
            }
        }
		#endregion
    }
}
